<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PlayerController extends Controller
{
    ////后台播放器分页列表
    public function playerlist()
    {
        $result = DB::table('player')
        ->where('status',1)
        ->paginate(5);
        return view('admin/player/list',['result'=>$result]);
    }

    //后台播放器搜索分页
    public function playerSelect(Request $request)
    {
        $result = DB::table('player')
        ->where('status',1)
        ->where('id','like','%'.$request->id.'%')
        ->paginate(5);

        return view('admin/player/list',['result'=>$result]);
    }

    //后台波泛起添加页
    public function playerAdd()
    {
        return view('admin.player.add');
    }

    public function playerInsert(Request $request)
    {
        // $result = $request->only(['name','cid','status','img']);
        // dd($result);

        if(!$request->hasFile('img') || !$request->has('name') || !$request->has('cid') || !$request->has('status')){
            return back()->withInput();
        }
        
        //接收表单数据
        $result = $request->only(['name','cid','status','img']);

        //进行文件上传
        $file=$request['img'];
        $filename=uniqid().'.'.$file->getClientOriginalExtension();
        //进行文件移动       
        $file->move('./uploads/',$filename);


        //插入数据库中
        $result = DB::table('player')->insert([
            ['name' => $result['name'], 'cid' => $result['cid'], 'status' => $result['status'],'img' => $filename]
        ]);
        if(!$result){
            return back()->withInput();
        }
        return redirect('/admin/player');
    }

    //修改轮播页面
    public function playerUpdate(Request $request, $id)
    {   
        //获取单个商品信息
        $result = DB::table('player')
        ->select('id','name','cid','img')
        ->where('id',$id)
        ->first();

        //dd($result);
       
        return view('admin/player/update',['result' => $result]);
    }

    public function doPlayerUpdate(Request $request)
    {  
        //获取表单提交的数据
        $result = $request->only(['name','cid','img','id']);
        // dd($result);
        if($request->hasFile('img')){
            //进行文件上传
            $fileimg = $request->file('img');
            $filename = uniqid().'.'.$fileimg->getClientOriginalExtension();
            // dd($filename);
            $fileimg->move('./uploads',$filename);

            //插入数据库
            $result1 = DB::table('player')
            ->where('id',$result['id'])
            ->update(
                ['name'=>$request['name'],'cid'=>$request['cid'],'img'=>$filename]
            );
            // $result1==1?return redirect('admin/goodslist');:return back()->withInput();
            if($result1){
                return redirect('admin/player');
            }else{
                return back()->withInput();
            }
        }else{
            $result2 = DB::table('goods')
            ->where('id',$result['id'])
            ->update(
                ['name'=>$request['name'],'cid'=>$request['cid']]
            );

            if($result2){
                return redirect('admin/player');
            }else{
                return back()->withInput();
            }
        }

    }


    //轮播的加入回收站管理
    public function playerRecycle($id)
    {
        
        $st = DB::table('player')
        ->where('id',$id)
        ->value('status');

        if($st==0){
            $status=1;
        }else{
            $status=0;
        }

        $result = DB::table('player')
        ->where('id',$id)
        ->update(
            ['status'=>$status]
        );

        if($result){
            return '1';
        }else{
            return '2';
        }
    }


    //回收站页面
    public function Recycle()
    {
       $result = DB::table('player')
        ->where('status',0)
        ->paginate(5);
        return view('admin/player/Recycle',['result'=>$result]); 
    }

}
